home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / SCHMOO.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  10KB  |  366 lines

  1. /*
  2.  * SCHMOO.C
  3.  *
  4.  * Basic Windows application code, with only a few specifics for handling
  5.  * blocking as far as OLE goes.  Most OLE interaction is directed through
  6.  * other functions in other source files that actually make the OLE library
  7.  * calls.
  8.  *
  9.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  10.  *
  11.  */
  12.  
  13. #include <windows.h>
  14. #include <ole.h>
  15. #include "schmoo.h"
  16. #include "oleglobl.h"
  17.  
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  * Array of pointers to strings loaded from the resource file.
  24.  * Pointers can be near since we'll use LocalAlloc for
  25.  * the string space.
  26.  */
  27.  
  28. char NEAR   *rgpsz[CSTRINGS];
  29.  
  30. //Global variable block.
  31. GLOBALS     stGlobals;
  32. LPGLOBALS   pGlob=&stGlobals;
  33.  
  34.  
  35.  
  36.  
  37. /*
  38.  * WinMain
  39.  *
  40.  * Purpose:
  41.  *  Main entry point of application.   Should register the app class
  42.  *  if a previous instance has not done so and do any other one-time
  43.  *  initializations.
  44.  *
  45.  * Parameters:
  46.  *  See Windows SDK Guide to Programming, page 2-3
  47.  *
  48.  * Return Value:
  49.  *  Value to return to Windows--termination code.
  50.  *
  51.  */
  52.  
  53. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  54.                     LPSTR lpszCmdLine, int nCmdShow)
  55.     {
  56.     HANDLE      hAccel;
  57.     HWND        hWnd;
  58.     MSG         msg;
  59.  
  60.  
  61.     pGlob->hInst=hInstance;
  62.     pGlob->pszCmdLine=lpszCmdLine;
  63.     pGlob->nCmdShow=nCmdShow;
  64.  
  65.     /*
  66.      * Try to initialize; on failure, clean up anything that might have
  67.      * been allocated.
  68.      */
  69.     if (!FApplicationInit(pGlob, hPrevInstance))
  70.         {
  71.         FApplicationExit(pGlob);
  72.         return FALSE;
  73.         }
  74.  
  75.     hWnd=CreateWindow(rgpsz[IDS_CLASSSCHMOO],
  76.                       rgpsz[IDS_CAPTION],
  77.                       WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW,
  78.                       CW_USEDEFAULT, CW_USEDEFAULT, 400, 350,
  79.                       NULL, NULL, hInstance, NULL);
  80.  
  81.     if (NULL==hWnd)
  82.         {
  83.         FApplicationExit(pGlob);
  84.         return FALSE;
  85.         }
  86.  
  87.     //Prevent the WM_SIZE from ShowWindow from making us dirty.
  88.     pGlob->fNoDirty=TRUE;
  89.     ShowWindow(hWnd, pGlob->nCmdShow);
  90.     pGlob->fNoDirty=FALSE;
  91.  
  92.     if (SW_HIDE != pGlob->nCmdShow)
  93.         UpdateWindow(hWnd);
  94.  
  95.     hAccel=LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATORS));
  96.  
  97.     //Non-OLE message loop.
  98.     while (GetMessage(&msg, NULL, 0,0 ))
  99.         {
  100.         if (!TranslateAccelerator(hWnd, hAccel, &msg))
  101.             {
  102.             TranslateMessage(&msg);
  103.             DispatchMessage(&msg);
  104.             }
  105.         }
  106.  
  107.     FApplicationExit(pGlob);
  108.     return msg.wParam;
  109.     }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. /*
  116.  * SchmooWndProc
  117.  *
  118.  * Purpose:
  119.  *  Window class procedure.  Standard callback.
  120.  *
  121.  * Parameters:
  122.  *  The standard.
  123.  *
  124.  * Return Value:
  125.  *  The standard.
  126.  *
  127.  */
  128.  
  129. long FAR PASCAL SchmooWndProc(HWND hWnd, UINT iMsg, UINT wParam, LONG lParam)
  130.  
  131.     {
  132.     static BOOL fNoSizePoly;
  133.     RECT        rc;
  134.     BOOL        fOK;
  135.     WORD        wTemp;
  136.     DWORD       dwStyle;
  137.     FARPROC     lpfn;
  138.     LPPOINT     ppt;
  139.  
  140.  
  141.     switch (iMsg)
  142.         {
  143.         case WM_CREATE:
  144.             //Set global variable defaults that pertain to this window.
  145.             pGlob->hWnd=hWnd;
  146.             pGlob->fOpenFile=FALSE;
  147.             pGlob->fNoDirty=FALSE;
  148.  
  149.             FDirtySet(FALSE);
  150.  
  151.             //Create the secondary edit window.
  152.             pGlob->hWndPolyline=HPolylineWindowCreate(hWnd, pGlob->hInst);
  153.  
  154.             //Set the initial window title.
  155.             WindowTitleSet(pGlob->hWnd, rgpsz[IDS_UNTITLED]);
  156.  
  157.             if (!FFileInit(pGlob))
  158.                 PostMessage(hWnd, WM_CLOSE, 0, 0L);
  159.  
  160.             fNoSizePoly=FALSE;
  161.             break;
  162.  
  163.  
  164.         case WM_SIZE:
  165.             /* If we are getting WM_SIZE in response to a Polyline
  166.              * notification, then don't resize it again.
  167.              */
  168.  
  169.             if (fNoSizePoly)
  170.                 break;
  171.  
  172.             //Resize the polyline editor window to fit the new client area.
  173.             GetClientRect(hWnd, &rc);
  174.             InflateRect(&rc, -8, -8);
  175.  
  176.             //Tell the Polyline window to resize without notifying us.
  177.             SendMessage(pGlob->hWndPolyline, PLM_RECTSET, FALSE,
  178.                         (LONG)(LPSTR)&rc);
  179.  
  180.             /*
  181.              * We consider sizing something that makes the file dirty, but
  182.              * not until we've finished the create process, which is why
  183.              * we set fDirty to FALSE after the ShowWindow above, because
  184.              * ShowWindow sends a WM_SIZE when the window first appears.
  185.              */
  186.             FDirtySet(TRUE);
  187.             break;
  188.  
  189.         case WM_GETMINMAXINFO:
  190.             //Limit the smallest tracking size allowable.
  191.             ppt=(LPPOINT)lParam;
  192.  
  193.             ppt[3].x=158;
  194.             ppt[3].y=168;
  195.             break;
  196.  
  197.         case WM_CLOSE:
  198.             if (!FFileExit(pGlob))
  199.                 break;
  200.  
  201.             DestroyWindow(hWnd);    //Same as DefWindowProc's action.
  202.             break;
  203.  
  204.  
  205.         case WM_DESTROY:
  206.             PostQuitMessage(0);
  207.             break;
  208.  
  209.  
  210.         case WM_INITMENUPOPUP:
  211.             /*
  212.              * Check for Save possibility in File menu position 0.
  213.              * In and OLE situation, this is always enabled.
  214.              */
  215.             if (!pGlob->fOLE && 0==LOWORD(lParam))
  216.                 {
  217.                 wTemp=(pGlob->fOpenFile) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  218.                 EnableMenuItem((HMENU)wParam, IDM_FILESAVE, wTemp | MF_BYCOMMAND);
  219.                 }
  220.  
  221.             //Check for possibility of Paste for edit menu position 1
  222.             if (1==LOWORD(lParam))
  223.                 {
  224.                 fOK=IsClipboardFormatAvailable(pGlob->cfSchmoo);
  225.                 wTemp=(fOK) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED);
  226.                 EnableMenuItem((HMENU)wParam, IDM_EDITPASTE, wTemp | MF_BYCOMMAND);
  227.                 }
  228.  
  229.             break;
  230.  
  231.  
  232.         case WM_COMMAND:
  233.             switch (wParam)
  234.                 {
  235.                 case ID_POLYLINE:
  236.                     if (PLN_POINTCHANGE==HIWORD(lParam))
  237.                         {
  238.                         FDirtySet(TRUE);
  239.                         break;
  240.                         }
  241.  
  242.                     /*
  243.                      * Polyline window is informing us that it changed size
  244.                      * in response to setting it's data.  Therefore we
  245.                      * have to size ourselves accordingly.
  246.                      */
  247.                     if (PLN_SIZECHANGE==HIWORD(lParam))
  248.                         {
  249.                         //Calculate new DIMENSIONS (we won't move)
  250.                         GetWindowRect(pGlob->hWndPolyline, &rc);
  251.                         InflateRect(&rc, 8, 8);
  252.  
  253.                         dwStyle=GetWindowLong(pGlob->hWnd, GWL_STYLE);
  254.                         AdjustWindowRect(&rc, dwStyle, TRUE);
  255.  
  256.                         fNoSizePoly=TRUE;
  257.                         SetWindowPos(pGlob->hWnd, NULL, 0, 0,
  258.                                      rc.right-rc.left, rc.bottom-rc.top,
  259.                                      SWP_NOMOVE | SWP_NOZORDER);
  260.                         fNoSizePoly=FALSE;
  261.                         FDirtySet(TRUE);
  262.                         }
  263.                     break;
  264.  
  265.  
  266.                 case IDM_FILENEW:
  267.                     FFileNew(pGlob);
  268.                     break;
  269.  
  270.  
  271.                 case IDM_FILEOPEN:
  272.                     FFileOpen(pGlob, FALSE);
  273.                     break;
  274.  
  275.  
  276.                 case IDM_FILESAVE:
  277.                     FFileSave(pGlob);
  278.                     break;
  279.  
  280.  
  281.                 case IDM_FILESAVEAS:
  282.                     fOK=FFileSaveAs(pGlob);
  283.                     return MAKELONG(fOK, 0);
  284.  
  285.  
  286.                 case IDM_FILEIMPORT:
  287.                     FFileOpen(pGlob, TRUE);
  288.                     break;
  289.  
  290.  
  291.                 case IDM_FILEEXIT:
  292.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  293.                     break;
  294.  
  295.  
  296.                 case IDM_EDITUNDO:
  297.                     SendMessage(pGlob->hWndPolyline, PLM_BACKUPUNDO, 0, 0L);
  298.                     break;
  299.  
  300.                 case IDM_EDITCUT:
  301.                     FEditCut(pGlob);
  302.                     break;
  303.  
  304.                 case IDM_EDITCOPY:
  305.                     FEditCopy(pGlob, FALSE);
  306.                     break;
  307.  
  308.                 case IDM_EDITPASTE:
  309.                     FEditPaste(pGlob);
  310.                     break;
  311.  
  312.  
  313.                 case IDM_HELPABOUT:
  314.                     lpfn=MakeProcInstance(AboutProc, pGlob->hInst);
  315.                     DialogBox(pGlob->hInst, MAKEINTRESOURCE(IDD_ABOUT),
  316.                               pGlob->hWnd, lpfn);
  317.                     FreeProcInstance(lpfn);
  318.                     break;
  319.                 }
  320.             break;
  321.  
  322.         default:
  323.             return (DefWindowProc(hWnd, iMsg, wParam, lParam));
  324.         }
  325.  
  326.     return 0L;
  327.     }
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. /*
  335.  * AboutProc
  336.  *
  337.  * Purpose:
  338.  *  Dialog procedure for the omnipresent About box.
  339.  *
  340.  * Parameters:
  341.  *  The standard.
  342.  *
  343.  * Return Value:
  344.  *  The value to be returned through the DialogBox call that
  345.  *  created the dialog.
  346.  *
  347.  */
  348.  
  349. BOOL FAR PASCAL AboutProc(HWND hDlg, UINT iMsg, UINT wParam, LONG lParam)
  350.     {
  351.     switch (iMsg)
  352.         {
  353.         case WM_INITDIALOG:
  354.             return TRUE;
  355.  
  356.         case WM_COMMAND:
  357.             switch (wParam)
  358.                 {
  359.                 case IDOK:
  360.                     EndDialog(hDlg, TRUE);
  361.                 }
  362.             break;
  363.         }
  364.     return FALSE;
  365.     }
  366.